﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuXiaChu.OrderDishes.BLL;
using BuXiaChu.OrderDishes.BLL.Constants;
using BuXiaChu.OrderDishes.Model;

namespace BuXiaChu.OrderDishes.Web.Admin.Restaurant
{
    public partial class AddDishes : System.Web.UI.Page
    {
        protected List<ShareCodeModel> unit = null;
        protected List<DishesCatModel> dishesCat = null;
        protected int restaurantId = 0;
        protected string message = "-1";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                InitData();
            }
            else
            {
                Save();
            }
        }

        private void InitData()
        {
            #region
            int.TryParse(Request.QueryString["restaurantId"], out restaurantId);

            unit = CodeDictionary.Instance.GetListByClassID(CodeDictionaryConstants.DishesUnit);
            foreach (var item in unit)
            {
                ListItem li = new ListItem(item.Name, item.CodeID.ToString());
                sel_Unit.Items.Add(li);
            }

            dishesCat = RestaurantManager.GetDishesCatByRestaurantId(restaurantId);
            foreach (var item in dishesCat)
            {
                ListItem li = new ListItem(item.Name, item.CatID.ToString());
                sel_Cat.Items.Add(li);
            }
            #endregion

            #region
            long dishesId = 0;
            if (long.TryParse(Request.QueryString["id"], out dishesId))
            {
                DishesModel entity = RestaurantManager.GetDishesById(dishesId);
                txt_Name.Value = entity.Name;
                sel_Cat.Value = entity.CodeId.ToString();
                txt_Price.Value = entity.Price.ToString("F2");
                sel_Unit.Value = entity.Unit.ToString();
                txt_Discount.Value = entity.Discount.ToString("F0");
                txt_InvoiceDiscount.Value = entity.InvoiceDiscount.ToString("F0");
                txt_Box.Value = entity.Box.ToString();
                txt_BoxUnitPrice.Value = entity.BoxUnitPrice.ToString("F0");

                txt_Image.Attributes.Add("oldvalue", entity.ImageName);
                if (entity.Image.Length > 0)
                {
                    img_PreviewImage.Src = "/ImageShow/Get.ashx?id=" + entity.CodeId;
                    //img_PreviewImage.Visible = true;
                }
                txt_Description.Value = entity.Description;
                txt_InternalRemarks.Value = entity.InternalRemarks;
            }
            #endregion
        }

        private void Save()
        {
            #region
            long dishesId = 0;
            int restaurantId = 0;
            long.TryParse(Request.QueryString["id"], out dishesId);
            if (int.TryParse(Request.QueryString["restaurantId"], out restaurantId))
            {
                DishesModel entity = new DishesModel();
                entity.CodeId = dishesId;
                entity.RestaurantId = restaurantId;
                entity.Name = txt_Name.Value;
                entity.CatID = int.Parse(sel_Cat.Value);
                entity.Price = decimal.Parse(txt_Price.Value);
                entity.Unit = int.Parse(sel_Unit.Value);
                entity.Discount = int.Parse(txt_Discount.Value);
                entity.InvoiceDiscount = int.Parse(txt_InvoiceDiscount.Value);
                if (string.IsNullOrEmpty(txt_Box.Value))
                {
                    txt_Box.Value = "0";
                }
                entity.Box = int.Parse(txt_Box.Value);
                if (string.IsNullOrEmpty(txt_BoxUnitPrice.Value))
                {
                    txt_BoxUnitPrice.Value = "0";
                }
                entity.BoxUnitPrice = decimal.Parse(txt_BoxUnitPrice.Value);

                string fileName = System.IO.Path.GetFileName(this.Request.Files["txt_Image"].FileName);
                entity.ImageName = txt_Image.Attributes["oldvalue"] ?? fileName;
                using (System.IO.Stream stream = this.Request.Files["txt_Image"].InputStream)
                {
                    byte[] fileContent = new byte[stream.Length];
                    stream.Read(fileContent, 0, (int)stream.Length);
                    entity.Image = fileContent;
                }
                entity.Description = txt_Description.Value;
                entity.InternalRemarks = txt_InternalRemarks.Value;

                if (RestaurantManager.Save(entity))
                {
                    if (entity.Image.Length > 0)
                    {
                        ImageShowManage.Dishes.SetImageToCache(entity.CodeId.ToString(), entity.Image);
                        img_PreviewImage.Src = "/ImageShow/Get.ashx?id=" + entity.CodeId;
                    }
                    message = "1";
                }
                else
                {
                    message = "0";
                }
            }
            #endregion
        }
    }
}
